import Result from "@/bean/vo/Result";
import axios, { AxiosRequestConfig } from "axios";

export type HttpMethod = "get" | "post" | "put" | "delete";

/** axios request 请求 */
export async function axiosRequest<T>(url: string, method: HttpMethod, params?: any, data?: any, config?: AxiosRequestConfig) {
  switch (method) {
    case "get":
      return axios.get<T>(url, { ...config, params, data });
    case "post":
      return axios.post<T>(url, data, { ...config, params });
    case "put":
      return axios.put<T>(url, data, { ...config, params });
    case "delete":
      return axios.delete<T>(url, { ...config, params, data });
    default:
      throw new Error("param [method] does not exist [RequestMethod]");
  }
}

export default async function request<T>(url: string, method: HttpMethod, params?: any, data?: any, config?: AxiosRequestConfig) {
  const response = await axiosRequest<Result<T>>(url, method, params, data, config);
  const result = response.data;
  return result.data;
}
